#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QPalette>
#include <QtSerialPort/QSerialPort>

#include "clickedLabel.h"
#include "projectdb.h"
#include "pages.h"
#include "cappacket.h"
#include "scl_reader.h"
#include "totaldataprocess.h"
#include "qpixeltool.h"
#include "udp.h"
#include "tcpserver.h"
//#include "keyboard/keywidget.h"
#include "F_WCPage.h"  //LM 非同步测量
#include "F_clnp.h" //LM 非同步测量

#define iZHOUQI 10       //周期

//namespace Ui {
//    class MainWindow;
//}
//QT_BEGIN_NAMESPACE
//class QListWidget;
//class QListWidgetItem;
//class QStackedWidget;
//QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    void makeMainWidget();
    ~MainWindow();

protected:
    bool eventFilter(QObject *obj, QEvent *event);
//    void mouseMoveEvent(QMouseEvent *e);
    void mousePressEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *e);
    void paintOnLubo(QWidget *w);
    void f_paintOnLubo(QWidget *w);//LM 非同步测量
    void paintOnUdp(QWidget *W);
    void f_paintOnUdp(QWidget *W);
    void findSmvMax();

private:
    QListWidget *contentsWidget;
    QStackedWidget *pagesWidget;
    QProcess    packet_analyzer;

    void loadPortConfig();

private slots:
    void on_btnMenu_Close_clicked();
    void on_btnPhoto_clicked();
    void slotBlack();
    void slotBlue();
    void slotGray();
    void slotNavy();

    void showBcdata();
    void slotLuboData();
    void slotOpenNetTimer();

    void paraReturn();
    void wcsjPage();
    void bwStart();
    void bwStop();
    void showBwinfo();

    void sysmanager();
    void sysOK();
    void sysCancel();
    void udpOrder(int);
    void serialCmd(const QByteArray &data);
    void bzselect();
    void bcselect();
    void changeset();
    void changeCyfs();
    void changeSaveFlag();
    void saveWcdata();
    void slotBtnTest();
    void paraOK();
    void NetTest();
    void StartLuBo();
    void flushBuff();
    void tabchanged();

  //  void sendContent(const QString&);
    void tdSelect();

    void dataFirst();
    void dataPrevious();
    void dataNext();
    void dataLast();
    void dataFind();

    void delWcdata();

    void startTimer();
    void stopTimer();
    void showDelAllMsg();
    void hideInfo();

    void showXiebonum();
    void toUpdate();

    void scrllCurveScroll();
    void tTest();
    void changeys(ClickedLabel*);
    void luboSelect();
    void selectTd(bool flag);
    void allPage();
    void startAll();

    void NextPage();
    void PrePage();

    void showDelFrm();
    void hideDelFrm();
    void delSelect();
    void showEdit(QModelIndex);
    void tabSysChanged();
    void saveWcbc();

    void fdjTimerOut();

    void slotTestEnd();
    void slotSyncSet(int);
    void slotUdp();
    void slotShowList();
    void slottemp1();
    void slottemp2();
    void slottemp3();
  //  void slotUdpDataIsOK();
    void showDelayText();
    void slotTcpProcess(int);
    void slotTcpInfo(int);

    void setMac();
    void jsWc();
    void jsUdpWc();
    void settempbuff();
    void addList(QString,QString,QString,int,int); //显示网络列表
    void slotShowMenu(QModelIndex);
    void slotShowScl();
    void clearFocusAll();
    void addTdh();
    void slotDs();
    void slotYs();
    //LM 非同步测量
    void f_bzselect();
    void f_bcselect();
    void f_changeset();
    void f_changeCyfs();
    void f_changeSaveFlag();
    void f_saveWcdata();
    void f_paraReturn();
    void f_paraOK();
    void parsePacket(const u_char *recvBuf, int recvLen);
    void f_jsWc();
    void s_showMUYS();
    void f_settempbuff();
    void f_slotTestEnd();
    void f_tdSelect();
    void f_slotShowList();
    void f_slotShowScl();
    //LM  非同步测量

    void setTestSelect(bool);
signals:
        void f_sigTestEnd();//LM  非同步测量
        void sigTestEnd();
        void sigSyncSet(int);
private:
    bool sourceIsActive;
    bool luboIsEnd;
    QSerialPort *serial;
    UdpClient *udp;
    TcpServer *tcpServer;
    CapPacket *cappacket;
    TestPage *newTestPage;
  //  ParaPage *newParaPage;
    LuboPage *newLuboPage;
    SysPage *newSysPage;
    WcPage *newWcPage;
 //   Ft3Page *newFt3Page;
    BwjyPage *newBwjyPage;
    AllPage *newAllPage;
    SclPage *newSclPage;
    bool f_calculateAvg();//LM 非同步测量
    void f_changeData();//LM 非同步测量
    void f_setparamsg();//LM 非同步测量
    F_WcPage *newF_WCPage;//LM 非同步测量 Page
    scl_reader reader;
    TotalDataProcess *tDataProcess;
    QTimer *timer;
    QTimer *captimer;
    QTimer *delShowtimer;
    QTimer *showInfoTimer;
    QTimer *bwTimer;
    QTimer *showAllTimer;
    QTimer *fdjTimer;
    QTimer *udpTimer;
    QTimer *dsTimer;
    QTimer *ysTimer;
    QTimer *openNetTimer;
    bool bmstart;
    bool StartFlag;
    double dblFuZhi_HGQ;
    int showIndex;
    int bzorbc;
    QString comstr;
    bool isComTest;
    bool isTested;
    QString guiyue;
    bool openNetFlag;

    int pwmfd;
    bool bee;
    void openPWM();
    void closePWM();

    void init();//参数初始化
    void setFrmLayout();//窗体布局
    void changeData();
    QString formatString(QString s);
 //   void sendCommitString(QString);
    void setparamsg();
    void initTest(); // 删除错误测量次数
    bool calculateAvg();
    int m_bIsConn;

    int m_iBiJiaoCiShu;		// 比较次数
    int m_iTestTimes;   // 测量次数
    int m_iTestTimes_YouXiao;   // 有效测量次数
    int m_iTestTimes_Bc;
    int m_iTestTimes_Jc;
    int m_iTestTimes_BzYxz;
    int m_iTestTimes_BcYxz;
    int m_iTestTimes_BzXw;
    int m_iTestTimes_BcXw;
    int m_MaxSZL;

    int m_iAvgNum;        //平均次数
    double m_dblJC_Cnt;            // 角差之和，用于计算平均角差
    double m_dblBC_Cnt;            // 比差之和，用于计算平均比差
    double m_dblAvg_BC;         // 平均比差
    double m_dblAvg_JC;		// 平均角差
    double m_dblAll_BC[1000];
    double m_dblAll_JC[1000];
    double m_dblAll_avgBC[1000];
    double m_dblAll_avgJC[1000];

    double m_BC[100];
    double m_JC[100];
    int m_iTestNum;


    double m_dblAll_BcYxz[1000];
    double m_dblAll_BzYxz[1000];
    double m_dblAll_BcXw[1000];
    double m_dblAll_BzXw[1000];
    double m_dblMaxJC;
    double m_dblMinJC;
    double m_dblMaxBC;
    double m_dblMinBC;

    double m_dblYXZ_HGQ;
    double m_dblYXZ_JYY;
    double m_dblBC;			// 比差
    double m_dblXW_HGQ;
    double m_dblXW_JYY;
    double m_dblJC;			// 角差
    int m_Count_BigJC;//大角差
    double netYouXiaoZhi;
    double netXiangWei;
    bool ProcessData(double dblYXZ_HGQ, double dblYXZ_JYY, double dblXW_HGQ, double dblXW_JYY);
    double BFB(){return 100 * m_dblYXZ_JYY;}

    double bzMax;
    double bcMax;
    double bzYxz;
    double bcYxz;
    double bzXw;
    double bcXw;

    void createkeyboady();
    void showNull();

    QSqlQuery tbWcxx;
    QSqlQuery tbWcdata;
    void showWcdata();
    bool isSaved;
    QString nowXh;
    QString preBFD;
    QString infnan(QString);
    int clfs;
    int dataCount;
    int tbsx;

    void showLubo();
    void ChangeX_Text();
    int ys[12];
    double m_iPeriodCnt;		// 录波周期总数
    double m_dblShowPeriods;		// 绘图框显示的周期数
    int m_iStartPoint;		// 绘图起始点
    double m_dblMaxPointValue;		// 最大点值
    void setXB_ChannelNum();
    // 获得坐标数量级
    float GetZuoBiaoSLJ(float fNum);
    int GetFormatPoint(float fNum);
    int iTestMode;

    bool bStart;
    int iStart;
    double pWc;
    double temppWc;
    bool m_bData;

    double TransSZLToValue_CT(double);
    double TransSZLToValue_PT(double);
    int m_zqgs;   //周期个数
    bool bDbd;
    QString strlog;

    double bzxs[10];
    double bcxs[10];
    void setBzBcXs();
    void showAllMMS();

    double sysbc1;
    double sysbc2;
    double sysbc3;
    double sysjc1;
    double sysjc2;
    double sysjc3;

    int m_iDelayNum;
    double delayMin;
    double delayMax;
    double delayAvg;
    float lsMin;
    float lsMax;
    double m_dblBwAll;
    double m_iBwLost;
    double m_iBwError;

    double dds;
    double dsstemp;
    float dsMax;
    float ssMax;
    int iDsFlag; //0 对时,1 守时
    int iDsNum;
    int iSsNum;
    int iYsNum;
    int iDsNum_static;

    int nowtest;
    void tcpWcSetup();
    void tcpWcSetup_f();
    void tcpDsSetup();
    void tcpXbSetup();
    void tcpLbSetup();
    void tcpMuSetup();
    void tcpMnSetup();
    void tcpFt3Setup();
    void f_Init_F_para();
public:
    QPixelTool pt;
    int tempbuff[12][iZHOUQI*200];
  //  void settempbuff(double a[12][5*50*200],int);

    void setCSCS();
    ProjectDB *myDB;
    void setTestText(int);
     void f_setTestText(int);//LM 非同步测量
     void f_setCSCS();//LM 非同步测量
     void f_changeJD(QString);//new LM
    void jsMaxMin();

    double SZLB(int num,double*D_ADD);//数字滤波,num组double类型的数，去掉一个最大一个最小，再取平均
    double SZLBA(int num,double*D_ADD);//数字滤波升级版,num组double类型的数，当数字大于平均值一定的幅值后，直接跟随
    void changeJD(QString);
  //  void Sync(int);
    void sigBeginSync(int);

private:
    //Ui::MainWindow *ui;

    QLabel *labIcon ;
    QLabel *labTitle ;
    QPushButton *btnPhoto ;
    QPushButton *btnMenu ;
    QPushButton *btnClose ;

    QPoint mousePoint;
    bool mousePressed;
    bool max;
    QRect location;

    //void InitStyle();
 //   QStackedWidget *pagesWidget;
    int m_iFlag;
    char *cUdpSend;
    QString sName1;
    QString sName2;
    QString sName3;
    int m_index;

    QPoint windowPos,mousePos,dPos;
    float bzTestXs;
    float bcTestXs;
    int syncFlag;
    int m_iFuwei;
    double jdys_avg;;

    int bwFlag;
    void writeXBdata(int);
    void GetYXZ_XieBo(int, int);
    int getTdh();
    void systemLoad(QString);
    double dblxbYXZ[100];

    float bcdata[4];
    float jcdata[4];

    QWidget *delWidget;
    QRadioButton *rdoXh;
    QRadioButton *rdoSj;
    QRadioButton *rdoAll;
    QLabel *labXh;
    QLineEdit *txtXh;
    QLabel *labSj;
    QLineEdit *txtSj;
    QPushButton *btnDelOK;
    QPushButton *btnDelCancel;
    void createDelFrm();
    int dataFlag;

  //  QWidget *sysWidget;
  //  QListWidget *sysList;

    int btnFlag;
    QString APPID;
    QString MAC;
    QString CYPL;
    QString ASDU;
    void saveMuinfo();
    void showSclInfo();
    QStandardItemModel *model;
    int testnum;
    int yanshiNum;
    QTableView *tabWcbj;
    QStandardItemModel *modela;
    QWidget *wcbjWidget;
    //keyWidget *keywidget;

    int focusIn;

    QTimer *fakePPS;
    int fake_pps_count;

    QPushButton *btnWcTest;
    QPushButton *btnMuTest;
    QPushButton *btnSysSetup;
    void addLiang();

    AddPage *newAddPage;
    BwjyPage *newMuPage;
    ExPage *newExPage;
    QLabel *labInfo;
    QWidget *widgetInfo;

    void loadCfg();
    void saveCfg();
    void showWcText();
    int udpResendNum;

    bool packet_testing;
    int allChannelShow;
    double maxInChanel[12];

    QPen dataPen;

    //LM 非同步测量
    int unSync_start_point;
    Clnp *m_clnp;
    QStringList m_netDevNames;
    QStringList m_netDevDescs;
    int m_selNetIndex;
    QString errBuf;
    QSettings *configIni;
    int devNum;
    void f_showWcText();//f_wcPage->
    bool f_ProcessData(double dblYXZ_HGQ, double dblYXZ_JYY, double dblXW_HGQ, double dblXW_JYY);
    //LM 非同步测量
};

#endif // MAINWINDOW_H
